package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BProject;
import com.ruoyi.system.domain.BProjectTask;
import com.ruoyi.system.domain.vo.BProjectVo;
import com.ruoyi.system.domain.vo.BUserVo;
import com.ruoyi.system.service.BProjectService;
import com.ruoyi.system.service.BUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 项目管理
 * @author yangxz
 **/
@RestController
@RequestMapping("/business/project")
public class ProjectController extends BaseController{

    @Autowired
    BProjectService projectService;

    @Autowired
    BUserService userService;

    /**
     * 获取项目列表
     */
//    @PreAuthorize("@ss.hasPermi('dict:project:list')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @GetMapping("/list")
    public TableDataInfo list(BProject project)
    {
        startPage();
        List<BProjectVo> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 获取项目列表
     */
//    @PreAuthorize("@ss.hasPermi('dict:project:userData')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @GetMapping("/userData")
    public TableDataInfo userData(BProjectTask projectTask)
    {
        startPage();
        List<BUserVo> list = userService.selectUserDataByProjectId(projectTask.getProjectId());
        return getDataTable(list);
    }


    /**
     * 根据药企获取用户列表
     */
//    @PreAuthorize("@ss.hasPermi('dict:project:userList')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @GetMapping("/userList/{companyIds}")
    public TableDataInfo userList(@PathVariable Long[] companyIds)
    {
        List<BUserVo> list = projectService.selectUserListByCompanyIds(companyIds);
        return getDataTable(list);
    }
    /**
     * 根据项目Id获取详细信息
     */
//    @PreAuthorize("@ss.hasPermi('dict:project:query')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable Long projectId) {
        BProjectVo projectVo = projectService.selectProjectById(projectId);
        return success(projectVo);
    }

    /**
     * 新增项目
     */
//    @PreAuthorize("@ss.hasPermi('dict:project:add')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @Log(title = "新增项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BProjectVo projectVo)
    {
        return toAjax(projectService.saveVo(projectVo));
    }

    /**
     * 修改项目
     */
//    @PreAuthorize("@ss.hasPermi('dict:project:edit')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @Log(title = "修改项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BProjectVo projectVo)
    {
        return toAjax(projectService.updateVo(projectVo));
    }

    /**
     * 删除项目
     */
//    @PreAuthorize("@ss.hasPermi('dict:project:remove')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @Log(title = "删除项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds)
    {
        return toAjax(projectService.deleteProjectByIds(projectIds));
    }


}
